package view;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JLabel;

import model.GameConstants;
import ctf.model.Side;
import ctf.view.FieldView;
import ctf.view.GameViewInterface;

@SuppressWarnings("serial")
public class GameView extends CtfPanel {
	private FieldView gameView;
	private GameViewInterface model;
	private JLabel leftTeamScore;
	private JLabel rightTeamScore;

	public GameView(CtfFrame window, GameViewInterface model) {
		super(window);
		this.model = model;
		this.setBackground(Color.black);
		this.gameView = new FieldView(model);
		this.gameView.setModel(model);
		this.gameView.setBounds(0, 0, getWidth(), getHeight()-40);
		this.add(gameView);
		this.leftTeamScore = new JLabel("0");
		this.leftTeamScore.setFont(new Font("Arial", Font.BOLD, 32));
		this.leftTeamScore.setForeground(Color.red);
		this.add(this.leftTeamScore);
		this.rightTeamScore = new JLabel("0");
		this.rightTeamScore.setFont(new Font("Arial", Font.BOLD, 32));
		this.rightTeamScore.setForeground(Color.blue);
		this.add(this.rightTeamScore);
		Timer refreshRate = new Timer();
		refreshRate.schedule(new TimerTask() {
			@Override
			public void run() {
				gameView.refresh();
			}
		}, 0, GameConstants.VIEW_UPDATE_PERIOD);
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		updateScores();
		this.gameView.setBounds(0, 0, getWidth(), getHeight()-40);
		}

	private void updateScores() {
		this.leftTeamScore.setLocation((int)(getWidth()*0.1), getHeight()-37);
		this.leftTeamScore.setText(Integer.toString(model.score(Side.LEFT)));
		this.rightTeamScore.setLocation((int)(getWidth()*0.85), getHeight()-37);
		this.rightTeamScore.setText(Integer.toString(model.score(Side.RIGHT)));
	}
	

}
